package com.tupu.service.impl.dispatch;

import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tupu.entity.dispatch.DadsCzp;
import com.tupu.entity.dispatch.DadsCzpczx;
import com.tupu.entity.dispatch.DispatchOperTicketDetails;
import com.tupu.entity.dispatch.DispatchOperTicketPerformMsg;
import com.tupu.mapper.dispatch.DadsCzpMapper;
import com.tupu.mapper.dispatch.DadsCzpczxMapper;
import com.tupu.mapper.dispatch.DispatchOperTicketDetailsMapper;
import com.tupu.service.dispatch.OperTicketDetailsService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class OperTicketDetailsServiceImpl extends ServiceImpl<DispatchOperTicketDetailsMapper, DispatchOperTicketDetails> implements OperTicketDetailsService {


    @Resource
    private DadsCzpMapper dadsCzpMapper;

    @Resource
    private DadsCzpczxMapper dadsCzpczxMapper;

    @Override
    @DS("dfdz")
    public DispatchOperTicketDetails findAll(String ticket_id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("INST_ID", ticket_id);
        DadsCzp dadsCzp = dadsCzpMapper.selectOne(queryWrapper);
        DispatchOperTicketDetails details = new DispatchOperTicketDetails();
        details.setDetailId(dadsCzp.getInstId());
        details.setDrawer(dadsCzp.getNpr());
        details.setTicketNum(dadsCzp.getCzpbh());
        details.setTicketTime(dadsCzp.getNprTime());
        details.setTicketState(dadsCzp.getZt());
        details.setOperTask(dadsCzp.getCzmd());
        details.setReviewer(dadsCzp.getShr1());
        details.setExamineTime(dadsCzp.getShsj1());
        List<DispatchOperTicketPerformMsg> performMsgList = getDacsCzxzx(ticket_id);
        if (!CollectionUtils.isEmpty(performMsgList)) {
            details.setPerformStartTime(performMsgList.get(0).getPublishTime());
            details.setPerformEndTime(performMsgList.get(performMsgList.size() - 1).getFinishTime());
        }
        details.setMsg(performMsgList);
        return details;
    }

    @Override
    @DS("dfdz")
    public boolean saveOperTicketDetailsList(DispatchOperTicketDetails details) {
        DadsCzp dadsCzp = new DadsCzp();
        dadsCzp.setInstId(details.getDetailId());
        dadsCzp.setNpr(details.getDrawer());
        dadsCzp.setNprTime(details.getTicketTime());
        dadsCzp.setCzpbh(details.getTicketNum());
        dadsCzp.setZt(details.getTicketState());
        dadsCzp.setCzmd(details.getOperTask());
        dadsCzp.setShr1(details.getReviewer());
        dadsCzp.setShsj1(details.getExamineTime());
        if (StrUtil.isNotEmpty(dadsCzp.getInstId())) {
            dadsCzpMapper.updateById(dadsCzp);
        } else {
            dadsCzpMapper.insert(dadsCzp);
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("lsdx", dadsCzp.getInstId());
        // 先删除，再插入
        dadsCzpczxMapper.delete(queryWrapper);
        for (DispatchOperTicketPerformMsg msg : details.getMsg()) {
            DadsCzpczx dadsCzpczx = new DadsCzpczx();
            dadsCzpczx.setInstId(msg.getMsgId());
            dadsCzpczx.setLsdx(dadsCzp.getInstId());
            dadsCzpczx.setCznr(msg.getOperContent());
/*            dadsCzpczx.setXlsj(msg.getPublishTime());
            dadsCzpczx.setXlr(msg.getPublishPeople());
            dadsCzpczx.setZxsj(msg.getReceiveTime());
            dadsCzpczx.setZxr(msg.getReceiver());
            dadsCzpczx.setJssj(msg.getFinishTime());
            dadsCzpczx.setLogicIndex(msg.getNodeNum());*/
            dadsCzpczx.setCzdw(msg.getStation());

            dadsCzpczxMapper.insert(dadsCzpczx);
        }
        return true;
    }

    /**
     * 查询具体操作项目
     *
     * @param ticket_id
     * @return
     */
    public List<DispatchOperTicketPerformMsg> getDacsCzxzx(String ticket_id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("LSDX", ticket_id);
        queryWrapper.orderByAsc("CAST(LOGIC_INDEX as SIGNED)");
        List<DadsCzpczx> dadsCzpczxList = dadsCzpczxMapper.selectList(queryWrapper);
        List<DispatchOperTicketPerformMsg> msgList = new ArrayList<>();

        for (DadsCzpczx dadsCzpczx : dadsCzpczxList) {
            DispatchOperTicketPerformMsg performMsg = new DispatchOperTicketPerformMsg();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            
            performMsg.setDetailId(ticket_id);
            performMsg.setMsgId(dadsCzpczx.getInstId());
            performMsg.setOperContent(dadsCzpczx.getCznr());
            Date xlsj = dadsCzpczx.getXlsj();
            if (null != xlsj){
                String xlsj2 = sdf.format(xlsj);
                performMsg.setPublishTime(xlsj2);
            }
            performMsg.setPublishPeople(dadsCzpczx.getXlr());
            Date zxsj = dadsCzpczx.getZxsj();
            if (null != zxsj){
                String zxsj2 = sdf.format(zxsj);
                performMsg.setReceiveTime(zxsj2);
                performMsg.setPerformTime(zxsj2);
            }
            performMsg.setReceiver(dadsCzpczx.getZxr());
            Date jssj = dadsCzpczx.getJssj();
            if (null != jssj){
                String jssj2 = sdf.format(jssj);
                performMsg.setFinishTime(jssj2);
            }
            performMsg.setNodeNum(dadsCzpczx.getLogicIndex());
            performMsg.setStation(dadsCzpczx.getCzdw());
            msgList.add(performMsg);
        }
        return msgList;
    }
}
